<?php

use Helpers\gantti;

date_default_timezone_set('UTC');
setlocale(LC_ALL, "it_IT");

$gantti = new Gantti($data, array(
    'title' => Lang::get('messages.vehicle'),
    'cellwidth' => 20,
    'cellheight' => 20,
    'today' => true
        ));
$gantti->setData2($data2);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
        <script src="{{URL::to('assets/js/calendar/jquery.qtip-1.0.0-rc3.min.js')}}" type="text/javascript"></script>
        <script type="text/javascript">

        </script>

        <script type="text/javascript">
            function showBox(text, obj) {
                helpNode = document.createElement('div');
                helpNode.id = 'popBox';
                helpNode.setAttribute("href", "#");

                helpNode.setAttribute('class', 'popBox');
                helpNode.innerHTML = text;
                obj.appendChild(helpNode);
            }

            function hideBox() {
                node = document.getElementById('popBox');
                node.parentNode.removeChild(node);
            }
        </script>

        <style type="text/css">
            .popBox {
                position: absolute;
                z-index: 1;
                background: #cccccc;
                width: 600px;
                padding: 0em;
                border: 1px solid gray;
            }

            span {
                z-index: 1;
                color: red;
                font-weight: bold;
            }
        </style>
        <title>Rental Calendar</title>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="{{URL::to('assets/css/calendar/screen.css')}}" />
        <link rel="stylesheet" href="{{URL::to('assets/css/calendar/gantti.css')}}" />

    </head>
    <body>
        <?php
        if (isset($_POST['cars'])) {
            $selected = $_POST['cars'];
        } else {
            $selected = "Te gjitha";
        }
        echo("</p></p>");
        echo $gantti->findSelected($selected)->render();
        ?>
        {{Lang::get('calendar.today_given_cars') }}
        <?php
        //makinat qe jane dhene sot
        $resultComingToday = DB::select("Select r.*,c.* from rentals r inner join cars c on r.vehicle=c.id "
                        . "where r.status='CHECK_OUT' and r.starttime>='" . $toDayDate . "' and r.starttime<='" . $toDayDate2 . "'");

        $numToDayComingCars = count($resultComingToday);
        $indexForTodayComingCar = 0;

        while ($indexForTodayComingCar < $numToDayComingCars) {
            $description = $resultComingToday[$indexForTodayComingCar]->Description;
            $rentdays = $resultComingToday[$indexForTodayComingCar]->rentdays;
            echo($description . "&nbsp;" . "&nbsp;" . $rentdays . "Dite" . "<br>");


            $indexForTodayComingCar++;
        }
        echo"</p>";
        ?>
        {{ Lang::get('calendar.today_returned_cars')}}

        <?php
//makinat qe do vine sot
        $resultComingToday1 = DB::select("Select r.*,c.* from rentals r inner join cars c on r.vehicle=c.id "
                        . "where r.status='CHECK_OUT' and r.endtime<='" . $toDayDate2 . "' order by ENDTIME");

        $numToDayComingCars1 = count($resultComingToday1);
        $indexForTodayComingCar1 = 0;

        while ($indexForTodayComingCar1 < $numToDayComingCars1) {
            $description1 = $resultComingToday1[$indexForTodayComingCar1]->Description;
            $endtime = $resultComingToday1[$indexForTodayComingCar1]->ENDTIME;
            $dat = date("d-m-Y", strtotime($endtime));
            $dat1 = date("H:i:s", strtotime($endtime));
            echo($description1 . "&nbsp;" . "&nbsp;" . $dat . "&nbsp;" . "&nbsp;" . "<b>" . $dat1 . "</b>" . "<br>");
            $indexForTodayComingCar1++;
        }
        echo"</p>";
//makinat qe jane ne pune
        $numOfActiveCar = DB::select("Select count(r.id) as count from rentals r where  r.status='CHECK_OUT'");

        $numOutCars = $numOfActiveCar[0]->count;
        ?>
        {{ Lang::get('calendar.working_cars').' '.$numOutCars}}
        <?php
        echo("</p>");

//makinat qe jane ne parking
        $numOfParkingCarsResult = DB::select("Select count(c.id) as count from cars c");
        $rezulNum = $numOfParkingCarsResult[0]->count;
        $numInCars = $numOutCars - $rezulNum;

        echo"</p>";
        ?>

        {{ Lang::get('calendar.parked_cars')}}
        <?php
        echo(-1 * $numInCars);
        ?>
        <script type="text/javascript">
            function extractIDs(ids) {

                var id = new Array();
                var j = 0;

                for (var i = 0; i < ids.length; i++) {

                    if (i > 4) {
                        if (ids.charAt(i - 2) == '=' && ids.charAt(i - 3) == 'd' && ids.charAt(i - 4) == 'i' && ids.charAt(i - 1) == '"')
                            id[j] = id[j] + ids.charAt(i) + ids.charAt(i + 1) + ids.charAt(i + 2) + ids.charAt(i + 3) + ids.charAt(i + 4) + ids.charAt(i + 5) + ids.charAt(i + 6) + ids.charAt(i + 7) + ids.charAt(i + 8) + ids.charAt(i + 9);
                    }
                    if (ids.charAt(i) == 'c') {
                        id[j] = id[j].replace("undefined", "");
                        j++;

                    }
                }
                return id;
            }
            function getSelectionHtml() {
                var html = "";
                if (typeof window.getSelection != "undefined") {
                    var sel = window.getSelection();
                    if (sel.rangeCount) {
                        var container = document.createElement("div");
                        for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                            container.appendChild(sel.getRangeAt(i).cloneContents());
                        }
                        html = container.innerHTML;
                    }
                } else if (typeof document.selection != "undefined") {
                    if (document.selection.type == "Text") {
                        html = document.selection.createRange().htmlText;
                    }
                }
                return html;
            }
            var carIdTurnBack = new Array();
            var index = 0;
            var el;
            var enterEl = false;

            var clickedIdTurnBack = new Array();

            $(document).click(function(e) {
                if (clickedIdTurnBack.length > 0) {

                    var d = document.getElementById("data");
                    for (var i = 0; i < clickedIdTurnBack.length; i++) {

                        clickedIdTurnBack[i].className = "gantt-day";
                        d.removeChild(document.getElementById("actual" + i));

                    }
                }
                clickedIdTurnBack = new Array();

                if (enterEl == true) {

                    enterEl = false;
                    el.parent().removeClass('gantt-day today');
                    el.parent().addClass("gantt-day");

                    var d = document.getElementById("data");
                    d.removeChild(document.getElementById("actual"));
                }

                var ids = new Array();
                ids = extractIDs(getSelectionHtml());

                if (carIdTurnBack != null) {
                    for (var i = 0; i < carIdTurnBack.length; i++) {
                        document.getElementById(carIdTurnBack[i]).style.color = "#93A1A1";
                    }
                }
                if (ids.length > 0) {
                    index = 0;
                    for (var i = 0; i < ids.length - 1; i++) {
                        var idToFind = ids[i];
                        $('*[id*=d' + idToFind + ']:visible').each(function() {
                            var id = this.id;
                            var idOfCar = "";
                            for (var i = 0; i < id.length; i++) {

                                if (id.charAt(i) == ']') {
                                    break;
                                }
                                if (id.charAt(i) != ']' && id.charAt(i) != '[') {

                                    idOfCar = idOfCar + id.charAt(i);
                                }
                            }
                            carIdTurnBack[index] = idOfCar;
                            document.getElementById(idOfCar).style.color = "#FF0000";
                            index = index + 1;
                        });
                        var v = document.getElementById(ids[i]);
                        v.className = "gantt-day today";
                        var section = document.getElementById("data");
                        var child = document.createElement("time");
                        var left = v.offsetLeft;
                        left = left + 8;
                        child.style.top = "40px";
                        child.style.left = left + "px";
                        child.id = "actual" + i;
                        section.appendChild(child);
                        clickedIdTurnBack[i] = v;
                    }
                } else {
                    el = $(e.target);

                    enterEl = true;
                    var idToFind = $(el).attr("id");
                    index = 0;
                    $('*[id*=d' + idToFind + ']:visible').each(function() {
                        var id = this.id;
                        var idOfCar = "";
                        for (var i = 0; i < id.length; i++) {

                            if (id.charAt(i) == ']') {
                                break;
                            }
                            if (id.charAt(i) != ']' && id.charAt(i) != '[') {

                                idOfCar = idOfCar + id.charAt(i);
                            }
                        }
                        carIdTurnBack[index] = idOfCar;
                        document.getElementById(idOfCar).style.color = "#FF0000";
                        index = index + 1;

                    });
                    el.parent().removeClass('gantt-day');
                    el.parent().addClass("gantt-day today");

                    var section = document.getElementById("data");
                    var child = document.createElement("time");
                    var left = el.position().left;
                    left = left + 8;
                    child.setAttribute("style", "top:40px; left:" + left + "px;");
                    child.setAttribute("id", "actual");
                    section.appendChild(child);
                }
            });
            var at = [
                'bottom left', 'bottom right', 'bottom center',
                'top left', 'top right', 'top center',
                'left center', 'left top', 'left bottom',
                'right center', 'right top', 'right bottom', 'center'
            ],
                    my = [
                        'top left', 'top right', 'top center',
                        'bottom left', 'bottom right', 'bottom center',
                        'right center', 'right top', 'right bottom',
                        'left center', 'left top', 'left bottom', 'center'
                    ],
                    styles = [
                        'red', 'blue', 'dark', 'light', 'green', 'jtools', 'plain', 'youtube', 'cluetip', 'tipsy', 'tipped', 'bootstrap'
                    ];
            $(document).ready(function()
            {
                // Match all <A/> links with a title tag and use it as the content (default).
                $('strong[title]').qtip({style: {name: 'dark'}, position: {
                        my: 'top left',
                        target: 'mouse',
                        viewport: $(window), // Keep it on-screen at all times if possible
                        adjust: {
                            x: 10, y: 10
                        }
                    }});
            });
            function getSelectedOption() {
                $("#title option:selected").text();
            }

        </script>
    </body>
</html>



